define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'integral/bookstore/index',
                    add_url: 'integral/bookstore/add',
                    edit_url: 'integral/bookstore/edit',
                    del_url: 'integral/bookstore/del',
                    multi_url: 'integral/bookstore/multi',
                    import_url: 'integral/bookstore/import',
                    table: 'integral_bookstore',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'bookstore_name', title: __('Bookstore_name')},
                        {field: 'boss', title: __('Boss')},
                        {field: 'tel', title: __('Tel')},
                        {field: 'control', title: __('Control')},
                        {field: 'discount', title: __('Discount'), operate:'BETWEEN'},
                        {field: 'department.name', title: __('Department.name')},
                        {field: 'admin.username', title: __('Admin.username')},
                        {field: 'status_text', title: __('Status'),searchList: {"1":__('铺货'),"2":__('现款')}, formatter: Table.api.formatter.status},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
          var submitForm = function (ids, layero) {
            var options = table.bootstrapTable('getOptions');
            var columns = [];
            $.each(options.columns[0], function (i, j) {
              if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                columns.push(j.field);
              }
            });
            var search = options.queryParams({});
            $("input[name=search]", layero).val(options.searchText);
            $("input[name=ids]", layero).val(ids);
            $("input[name=filter]", layero).val(search.filter);
            $("input[name=op]", layero).val(search.op);
            $("input[name=columns]", layero).val(columns.join(','));
            $("form", layero).submit();
          };
          $(document).on("click", ".btn-export", function () {
            var ids = Table.api.selectedids(table);
            var page = table.bootstrapTable('getData');
            var all = table.bootstrapTable('getOptions').totalRows;
            console.log(ids, page, all);
            Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("integral/bookstore/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
              title: '导出数据',
              btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
              success: function (layero, index) {
                $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
              }
              , yes: function (index, layero) {
                submitForm(ids.join(","), layero);
                return false;
              }
              ,
              btn2: function (index, layero) {
                var ids = [];
                $.each(page, function (i, j) {
                  ids.push(j.id);
                });
                submitForm(ids.join(","), layero);
                return false;
              }
              ,
              btn3: function (index, layero) {
                submitForm("all", layero);
                return false;
              }
            })
          });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});